<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		 <meta name="renderer" content="webkit">
	    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>部门管理</title>
	</head>
	<link rel="stylesheet" href="__PUBLIC__/frame/layui/css/layui.css">
	<body>
		<button class="layui-btn" onclick="location.href='<?php echo U('add');?>'" style="margin-top: 10px;"><i class="layui-icon">&#xe654;</i>添加部门</button>
        <a class="layui-btn btn-add btn-default" id="btn-refresh" style="margin-top: 10px;"><i class="layui-icon">&#x1002;</i></a>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>部门列表</legend>
        </fieldset>
        <table class="layui-table">
            <colgroup>
                <col width="150">
                <col width="150">
                <col width="200">
                <col>
            </colgroup>
            <thead>
                <tr>
                    <th>排序</th>
                    <th>部门id</th>
                    <th>部门名称</th>
                    <th>部门父级ID</th>                  
                    <th>是否禁用</th>
                    <th>操作</th>
                </tr> 
            </thead>
            <?php foreach ($list as $key => $value):?>            
                <tbody class="numberClass">
                    <tr>
                        <td><input type="number" name="sort" part_id="{$value['id']}" value="{$value['sort']}" /></td>
                        <td>{$value['id']}</td>
                        <td>{$value['name']}</td>
                        <td>{$value['pid']}</td>
                        <td><?php echo $value['is_use']==1?'使用中':'已禁用';?></td>
                        <td>
                            <a class="layui-btn layui-btn-mini" href="<?php echo U('edit',array('id'=>$value['id']));?>"><i class="layui-icon">&#xe642;</i></a>
                            <button class="layui-btn layui-btn-danger layui-btn-mini" name="jinyong" part_id ="{$value['id']}" is_use="{$value['is_use']}"><?php echo $value['is_use']==1?'禁用':'恢复';?></button>
                        </td>
                    </tr>
                    <if condition="array_key_exists('child',$value)">
                        <?php foreach ($value['child'] as $k1 => $v1):?>
                            <tr>
                                <td><input type="number" name="sort" part_id="{$v1['id']}" value="{$v1['sort']}" /></td>
                                <td>{$v1['id']}</td>
                                <td>{$v1['name']}</td>
                                <td>{$v1['pid']}</td>
                                <td><?php echo $v1['is_use']==1?'使用中':'已禁用';?></td>
                                <td>
                                    <a class="layui-btn layui-btn-mini" href="<?php echo U('edit',array('id'=>$v1['id']));?>"><i class="layui-icon">&#xe642;</i></a>
                                    <button class="layui-btn layui-btn-danger layui-btn-mini" name="jinyong" part_id ="{$v1['id']}" is_use="{$v1['is_use']}"><?php echo $v1['is_use']==1?'禁用':'恢复';?></button>
                                </td>
                            </tr>
                            <if condition="array_key_exists('child',$v1)">
                                <?php foreach ($v1['child'] as $k2 => $v2):?>
                                    <tr>
                                        <td><input type="number" name="sort" part_id="{$v2['id']}" value="{$v2['sort']}" /></td>
                                        <td>{$v2['id']}</td>
                                        <td>{$v2['name']}</td>
                                        <td>{$v2['pid']}</td>
                                        <td><?php echo $v2['is_use']==1?'使用中':'已禁用';?></td>
                                        <td>
                                            <a class="layui-btn layui-btn-mini" href="<?php echo U('edit',array('id'=>$v2['id']));?>"><i class="layui-icon">&#xe642;</i></a>
                                            <button class="layui-btn layui-btn-danger layui-btn-mini" name="jinyong" part_id ="{$v2['id']}" is_use="{$v2['is_use']}"><?php echo $v2['is_use']==1?'禁用':'恢复';?></button>
                                        </td>
                                    </tr>
                                        <?php if(array_key_exists('child',$v2)): foreach ($v2['child'] as $k3 => $v3):?>
                                            <tr>
                                                <td><input type="number" name="sort" part_id="{$v3['id']}" value="{$v3['sort']}" /></td>
                                                <td>{$v3['id']}</td>
                                                <td>{$v3['name']}</td>
                                                <td>{$v3['pid']}</td>
                                                <td><?php echo $v3['is_use']==1?'使用中':'已禁用';?></td>
                                                <td>
                                                    <a class="layui-btn layui-btn-mini" href="<?php echo U('edit',array('id'=>$v3['id']));?>"><i class="layui-icon">&#xe642;</i></a>
                                                    <button class="layui-btn layui-btn-danger layui-btn-mini" name="jinyong" part_id ="{$v3['id']}" is_use="{$v3['is_use']}"><?php echo $v3['is_use']==1?'禁用':'恢复';?></button>
                                                </td>
                                            </tr>
                                        <?php endforeach; endif;?> 
                                <?php endforeach;?> 
                            </if>
                        <?php endforeach;?> 
                    </if>
                </tbody>
            <?php endforeach;?>
        </table>     			
    </body>
</html>
<script type="text/javascript" src="__PUBLIC__/frame/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['layer','element','table'], function () {
        // 操作对象
        var layer = layui.layer ,element = layui.element,table = layui.table,$ = layui.jquery;
        $('#btn-refresh').on('click', function () {
            location.reload();
        });
        $(document).on('click','button[name=jinyong]',function(){
            var part_id = $(this).attr('part_id');
            var is_use = $(this).attr('is_use');
            var $this = $(this);
            layer.confirm('确认要禁用该部门？', {
                btn: ['是','否'] //按钮
            }, function(){
                $.post('/index.php/Hawk/Part/disabled',{'id':part_id,'is_use':is_use},function(data){
                    var obj = eval('(' + data + ')');
                    console.log(obj);
                    if(obj.code==1){
                        $this.attr('is_use',1);
                        $this.html('禁用');
                        $this.parent().siblings().eq(3).html('使用中');
                    }else if(obj.code==2){
                        $this.attr('is_use',0);
                        $this.html('恢复');
                        $this.parent().siblings().eq(3).html('已禁用');
                    }
                    layer.msg(obj.msg);
                });
            }, function(){
                layer.msg('你已取消！');
            });
        });
        $(document).on('blur','input[name=sort]',function(){
            var part_id = $(this).attr('part_id');
            var sort = $(this).val();
            $.post('/index.php/Hawk/Part/sort',{'part_id':part_id,'sort':sort},function(data){
                var obj = eval('(' + data + ')');
                layer.msg(obj.msg);
            });
        });
    });
</script>